
package com.sanji.sjmallmodule.phoneshell;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.goods.SearchTypeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PhoneShellBrandAdapter extends BaseAdapter {
    private Context mContext;
    private List<PhoneShellBrandBean> mData = new ArrayList<>();
    private int clickTemp = -1;

    public PhoneShellBrandAdapter(Context mContex, List<PhoneShellBrandBean> mData) {
        this.mContext = mContex;
        this.mData = mData;
    }

    public void notify(List<PhoneShellBrandBean> mData){
        this.mData = mData;
        this.notifyDataSetChanged();
    }

    //标识选中的item
    public void setSelection(int position) {
        clickTemp = position;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder vHolder = null;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fragment_search_grideview_item, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }
        //点击
        // 改变选中listitem的背景颜色
        if (clickTemp == position) {
            vHolder.typeName.setBackgroundResource(R.drawable.search_text_select_rectangle);

            vHolder.typeName.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
        } else {
            vHolder.typeName.setBackgroundResource(R.drawable.search_text_unselect_rectangle);
            vHolder.typeName.setTextColor(mContext.getResources().getColor(R.color.color_7f7f7f));
        }
        //捆绑数据
        PhoneShellBrandBean brandBean = mData.get(position);
        if (brandBean!=null){
            vHolder.typeName.setText(brandBean.getName());
            //绑定tag
            vHolder.typeName.setTag(R.id.tag_first, position);
            vHolder.typeName.setTag(R.id.tag_second, view);
        }

        return view;
    }

    class ViewHolder {
        @BindView(R.id.search_type_tv)
        TextView typeName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
